package com.seu.me.zookeeper.zkclient.watcher;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

import java.util.List;

/**
 * @author liangfeihu
 * @since 2017/12/5 17:54.
 */
public class ZkClientWatcher1 {
    /**
     * zookeeper地址
     */
    static final String CONNECT_ADDR = "192.168.169.129:2181,192.168.169.130:2181,192.168.169.131:2181";
    /**
     * session超时时间 ms
     */
    static final int SESSION_TIMEOUT = 5000;

    /**
     * main 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_TIMEOUT);

        //对父节点添加监听子节点变化。
        zkc.subscribeChildChanges("/super1", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("parentPath: " + parentPath);
                System.out.println("currentChilds: " + currentChilds);
            }
        });

        Thread.sleep(3000);

        zkc.createPersistent("/super1");
        Thread.sleep(1000);

        zkc.createPersistent("/super1" + "/" + "c1", "c1内容");
        Thread.sleep(1000);

        zkc.createPersistent("/super1" + "/" + "c2", "c2内容");
        Thread.sleep(1000);

        zkc.delete("/super1/c2");
        Thread.sleep(1000);

        zkc.deleteRecursive("/super1");
        Thread.sleep(Integer.MAX_VALUE);

    }
}
